﻿using System;
using System.Collections.Generic;
using SQLite;

namespace Data
{
	public sealed class Database
	{
		static readonly Database _instance = new Database();

		private SQLiteConnection db;

		public static Database Instance
		{
			get { return _instance; }
		}

		private Database ()
		{
			db = new SQLiteConnection (Tools.GetDBPath());

			db.CreateTable<Aircraft> ();
			db.CreateTable<Flight> ();
			db.CreateTable<Battery> ();
		}

		#region aircraft
		public List<Aircraft> GetAllAircraft(){
			return new List<Aircraft> (db.Table<Aircraft> ());			
		}

		public Aircraft GetAircraft(int id){
			throw new System.NotImplementedException ();
		}

		public void AddOrUpdateAircraft(Aircraft aircraft){
			if (db.Find<Aircraft> (aircraft.Id) != null)
				db.Update (aircraft);
			else
				db.Insert (aircraft);
		}

		public void DeleteAircraft(Aircraft aircraft){
			db.Delete (aircraft);
		}
		#endregion

		#region flights
		public List<Flight> GetAllFlights(){
			throw new System.NotImplementedException ();
		}

		public List<Flight> GetFlightsByAircraft(int aircraftId){
			throw new System.NotImplementedException ();
		}

		public List<Flight> GetFlightsByBattery(int batteryId){
			throw new System.NotImplementedException ();
		}

		public void AddOrUpdateFlight(Flight flight){
			if (db.Find<Flight> (flight.Id) != null)
				db.Update (flight);
			else
				db.Insert (flight);
		}
		#endregion
	}
}

